package com.ilongsay.redisConfig;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.lang.reflect.Method;

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
	//protected final static Logger log = LoggerFactory.getLogger(RedisCacheConfig.class);

	private volatile JedisConnectionFactory mJedisConnectionFactory;
	private volatile RedisTemplate<String, String> mRedisTemplate;
	private volatile RedisCacheManager mRedisCacheManager;

	public RedisCacheConfig() {
		super();
	}

	public RedisCacheConfig(JedisConnectionFactory mJedisConnectionFactory, RedisTemplate<String, String> mRedisTemplate, RedisCacheManager mRedisCacheManager) {
		super();
		this.mJedisConnectionFactory = mJedisConnectionFactory;
		this.mRedisTemplate = mRedisTemplate;
		this.mRedisCacheManager = mRedisCacheManager;
	}

	public JedisConnectionFactory redisConnectionFactory() {
		return mJedisConnectionFactory;
	}

	/**
	 *@Createtime 2018-08-09 17:29
	 *@Description RedisTemplate配置   redis序列化
	 */
	public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
//		StringRedisTemplate template = new StringRedisTemplate(cf);
//		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//		ObjectMapper om = new ObjectMapper();
//		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//		jackson2JsonRedisSerializer.setObjectMapper(om);
//		template.setValueSerializer(jackson2JsonRedisSerializer);
//		template.afterPropertiesSet();
//		return template;
		return mRedisTemplate;
	}

	public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
		return mRedisCacheManager;
	}

	/**
	 *@Createtime 2018-08-09 17:23
	 *@Description 默认的key生成策略
	 * @return
	 */
	@Bean
	public KeyGenerator keyGenerator() {
		return new KeyGenerator() {

			@Override
			public Object generate(Object o, Method method, Object... objects) {
				StringBuilder sb = new StringBuilder();
				sb.append(o.getClass().getName());
				sb.append(method.getName());
				for (Object obj : objects) {
					sb.append(obj.toString());
				}
				return sb.toString();
			}
		};
	}
	/**
	 *@Createtime 2018-08-09 17:26
	 *@Description 自定义缓存生成策略1
	 */
	@Bean(name = "listkeyGenerator")
	public KeyGenerator listkeyGenerator(){
		return new KeyGenerator() {
			@Override
			public Object generate(Object o, Method method, Object... objects) {
				StringBuilder sb = new StringBuilder();
				sb.append(method.getName());
				return sb.toString();
			}
		};
	}
	/**
	 *@Createtime 2018-08-09 17:35
	 *@Description 自定义缓存生成策略2
	 */
	@Bean(name = "firstParamKeyGenerator")
	public KeyGenerator firstParamKeyGenerator(){
		return new KeyGenerator() {
			@Override
			public Object generate(Object o, Method method, Object... params) {
				StringBuilder sb = new StringBuilder();
				sb.append(params[0].toString());
				return sb.toString();
			}
		};
	}
}